<?php 
declare(strict_types=1);
namespace App\Service;

use App\Dao\AdminDao;
use App\Dao\AdminLogDao;
use Hyperf\Utils\Context;

class AdminLogService
 {

    /**
     * 获取管理员日志列表
     * @param $page
     * @param $pageSize
     * @param string $name
     * @param string $startDate
     * @param string $endDate
     * @return array
     */
     public function list($page, $pageSize, $name='', $startDate='', $endDate='')
     {

         $where = [];
         if($startDate){
             $where[] = ['admin_log.add_time','>=' ,strtotime($startDate)];
         }
         if($endDate){
             $where[] = ['admin_log.add_time','<' ,strtotime($endDate)];
         }

         $adminIds = [];
         if($name){
             $adminIds = make(AdminDao::class)->searchIdsByName($name);
             if(empty($adminIds)){
                 $where[] = ['admin_log.admin_id','=' ,-1];
             }
         }
         return make(AdminLogDao::class)->getListPage($where, $adminIds, (int)$page, $pageSize);

     }

    /**
     * 添加管理员操作日志
     * @param $operate
     * @param int $adminId
     * @return int
     */
     public function addLog($operate, $adminId = 0){

         if(empty($adminId)){
             $user = Context::get('jwt_token');
             $adminId = $user['uid'] ? $user['uid'] : 0;
         }

         return make(AdminLogDao::class)->add([
             'admin_id' => $adminId,
             'operate' => $operate,
             'add_time' => time()
         ]);
     }

 }